﻿define(['admin/app', "service/admin/declarationService"], function (app) {
    "use strict";

    app.controller('declarationController', function ($scope, $state, $stateParams, $location, utils, w5cValidator, declarationService) {
        $scope.psdisabled = false;
        $scope.query = { page: 1, size: 20, mobile: '', auth_status: '' };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                declarationService.gets($scope.query).success(function (response) { $scope.list = response.data; });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    declarationService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        $scope.model.company_type = response.data.company_type.toString();
                        $scope.model.auth_status = response.data.auth_status.toString();
                    });
                    declarationService.getentrepot().success(function (response) {
                        $scope.entrepot = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                    if ($stateParams.action == "audit") {
                        $scope.breadcrumb = '审核';
                        $scope.action = "audit";
                    }   
                }
                else {
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    name: { required: '请填写商户名称' },
                    mobile: { required: '请填写手机号码'}
                });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            code: function () {
                declarationService.getcode().success(function (response) {
                    $scope.code = response.data;
                });
            },
            get_company_type: function (id) {
                var company_type = new Array("全部", "贸易", "销售");
                return company_type[id];
            },
            get_audit_status: function (id) {
                var audit_status = new Array("待缴费", "待审核", "审核通过", "审核不通过");
                return audit_status[id];
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                if ($stateParams.id > 0) {
                  if ($stateParams.action != "audit")
                    declarationService.alter($scope.model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                      }).error(function () { $scope.psdisabled = false; });
                   else
                      declarationService.audit($scope.model).success(function (response) {
                          utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                          if (response.result_code == 1000)
                              methods.goBack();
                          else
                              $scope.psdisabled = false;
                      }).error(function () { $scope.psdisabled = false; });
                }
                else {
                    declarationService.post($scope.model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除当前商户", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    declarationService.delete(model.id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            utils.remove($scope.model, model);
                    })
                })
            }
        }
        angular.extend($scope, methods);
        if ($stateParams.action != undefined) {
            methods.code();
            methods.edit();
        }
        else
            methods.search();
    });
});
